package com.inifitness.message.processor.message;

import com.inifitness.common.event.event.MessageEvent;
import com.inifitness.common.model.NoticeMessage;
import com.inifitness.message.service.EventTemplate;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

/**
 * TODO: (sunjinfeng 2024-05-29)
 *
 * @author sunjinfeng
 * @version 1.0.0
 * @since 2024-05-29 12:50
 */
@Component
@Log4j2
@RequiredArgsConstructor
public class WebsocketProcessor implements MessageProcessor {
  private final SimpMessagingTemplate messagingTemplate;

  @Override
  public void process(MessageEvent event, EventTemplate template) {
    NoticeMessage<String> message = new NoticeMessage<>("系统消息",parseMessage(event, template.getTemplate()));
    noticeStore(template.getStoreId(),message);
    log.info(()-> MessageFormatter.format("消息内容：{}", parseMessage(event, template.getTemplate())));
  }


  /**
   * 广播发送消息
   * @param storeId 门店ID
   * @param message 消息
   * @return
   */
  public void noticeStore(Long storeId, NoticeMessage message) {
    messagingTemplate.convertAndSend("/topic/notice/"+storeId, message);
  }

  /**
   * 广播发送消息
   * @param message 消息
   * @return
   */
  @SuppressWarnings("unused")
  public void noticeAll(NoticeMessage message) {
    messagingTemplate.convertAndSend("/topic/notice/", message);
  }
}
